﻿using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class OtherMessageBLL
    {
        #region 构造单例
        private OtherMessageBLL() { }
        private static OtherMessageBLL _instance;
        public static OtherMessageBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new OtherMessageBLL());
            }
        }
        #endregion

        public ResultDto<OtherMessageModel>  Add(Othermessage info)
        {
            ResultDto<OtherMessageModel> result = new ResultDto<OtherMessageModel>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                ef.Othermessage.Add(info);
                ef.Entry<Othermessage>(info).State = EntityState.Added;
                ef.SaveChanges();
                var models=GetByPage(info.MessageId, null, null,null, new RequestPageBase() { PageIndex = 1,PageSize=1});
                if (models != null && models.Count > 0)
                {
                    result.FirstParam = models[0];
                }
                else
                {
                    result.Status = -1;
                    result.Message = "Add Failed,Please Verification Params";
                }
                return result;
            }
        }

        public ResultDto<List<OtherMessageModel>> Add(List<Othermessage> infos)
        {
            ResultDto<List<OtherMessageModel>> result = new ResultDto<List<OtherMessageModel>>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                List<OtherMessageModel> list = new List<OtherMessageModel>();
                foreach (var item in infos)
                {
                    ef.Othermessage.Add(item);
                    ef.Entry<Othermessage>(item).State = EntityState.Added;
                    ef.SaveChanges();
                    var models = GetByPage(item.MessageId, null, null, null, new RequestPageBase() { PageIndex = 1, PageSize = 1 });
                    if (models != null && models.Count > 0)
                    {
                        list.Add(models[0]);
                    }
                }
                result.FirstParam = list;
                return result;
            }
        }


        public ResultDto<OtherMessageModel> ChangeMessageStatus(int messageId, int status)
        {
            ResultDto<OtherMessageModel> result = new ResultDto<OtherMessageModel>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Othermessage.Where(p => p.MessageId == messageId).FirstOrDefault();
                if (exist == null)
                {
                    result.Status = -1;
                    result.Message = "Message Is Not Exist";
                    return result;
                }
                exist.ReceiveTime = DateTime.Now;
                exist.Status = status;
                ef.Othermessage.Add(exist);
                ef.Entry<Othermessage>(exist).State = EntityState.Modified;
                ef.SaveChanges();
                var models = GetByPage(messageId, null, null,null, new RequestPageBase() { PageIndex = 1, PageSize = 1 });
                if (models != null && models.Count > 0)
                {
                    result.FirstParam = models[0];
                }
                return result;
            }
        }

        public int Remove(int messageId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Othermessage.Where(p => p.MessageId == messageId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                ef.Othermessage.Add(exist);
                ef.Entry<Othermessage>(exist).State = EntityState.Deleted;
                return ef.SaveChanges();
            }
        }

        public List<OtherMessageModel> GetByPage(int? messageId,int? sendUserId,int? targetUserId,int? status, RequestPageBase page)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var sql = " select a.*,b.RealName as TargetUserName,d.RealName as SendUserName,c.DepName from othermessage as a ";
                sql += "  left join  userinfo as b on a.TargetUserID=b.UserID ";
                sql += "  left join department as c on a.DepID=c.DepID ";
                sql += "  left join  userinfo as d on a.SendUserID=d.UserID ";
                sql += " where 1=1 {0} ";
                string paramsSql = string.Empty;

                if (messageId.HasValue)
                {
                    paramsSql += $" and a.MessageID = {messageId.Value}";
                }
                if (sendUserId.HasValue)
                {
                    paramsSql += $" and a.SendUserID = {sendUserId.Value}";
                }
                if (targetUserId.HasValue)
                {
                    paramsSql += $" and a.TargetUserID = {targetUserId.Value}";
                }

                if (status.HasValue)
                {
                    paramsSql += $" and a.Status = {status.Value}";
                }


                if (!string.IsNullOrEmpty(page.Keyword))
                {
                    paramsSql += $"  b.RealName like '%{page.Keyword}%'";
                    paramsSql += $" or d.RealName like '%{page.Keyword}%'";
                    paramsSql += $" or a.Title like '%{page.Keyword}%'";
                    paramsSql += $" or a.Content like '%{page.Keyword}%'";
                    paramsSql = $" and ({paramsSql})";
                }
                var timeSql = page.TimeSql(" a.AddTime ");
                paramsSql += timeSql;
                sql = string.Format(sql, paramsSql);
                var countSql = sql.CountSql();
                page.TotalRows = Convert.ToInt32(ef.Database.SqlQuery(countSql).Rows[0][0]);
                paramsSql += page.OrderByPattern == 0 ? "a.AddTime".OrderByDescending() : "a.AddTime".OrderBy();
                paramsSql += page.Limit();
                var list = ef.Database.SqlQuery<OtherMessageModel>(sql);
                return list;
            }
        }



    }
}
